from urllib2 import urlopen, HTTPError
from BeautifulSoup import BeautifulStoneSoup
from photos import Photo

class Gallery():
	# do the stuff!
	def __init__(self,d):
		self._photos = None
		for k, v in d.items():
			setattr(self, k, v)
			
	@property
	def photos(self):
		if self._photos is None:
			self._load_photos()
		return self._photos
		
	def _load_photos(self):
		self._photos = []
		# retrieve the photos using the XML for this gallery
		try:
			xml = urlopen('%s' % self.xml_url).read()
		except HTTPError:
			raise Exception('Gallery not found - are you sure the username/slug is correct?')
		gallery_xml = BeautifulStoneSoup(xml,selfClosingTags=['photo'])
		for photo in gallery_xml.findAll(name='photo'):
			self._photos.append(Photo(photo._getAttrMap()))